package com.hk.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.hk.model.ProductMenu;
import com.hk.model.vo.Result;
import com.hk.model.vo.ResultVO;
import com.hk.service.IProductMenuService;
import com.hk.service.impl.ProductMenuServiceImpl;
import com.hk.utils.CharacterEncoding;
import com.hk.utils.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author: zhe
 * @Date: 2021/6/18 17:00
 * @Statement: 查询所有产品类型和新增
 */
@WebServlet("/product/ProductMenuQueryOrSave")
public class ProductMenuQueryOrSaveServlet extends HttpServlet {

    IProductMenuService iProductMenuService = new ProductMenuServiceImpl();

    /**
     * 查询产品类型
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        String page = Tool.nullToString(req.getParameter("page"));
        String limit = Tool.nullToString(req.getParameter("limit"));
        String name = Tool.nullToString(req.getParameter("name"));
        String gmtCreate = Tool.nullToString(req.getParameter("gmtCreate"));
        BufferedReader reader = req.getReader();
        ProductMenu   productMenu = new ProductMenu();
        productMenu.setName(name);
        productMenu.setGmtCreate(gmtCreate);

        int i = Tool.parseInt(page);
        int size = Tool.parseInt(limit);
        //        根据sql语句规则将每页页码修改为需要数值
        int pageNum = Tool.pageNum(i,size);
//        查询数据
        List<ProductMenu> productMenuList = iProductMenuService.findAll(pageNum, size, productMenu);
//        查询总数
        int count = iProductMenuService.count(pageNum,size, productMenu);
        PrintWriter out = resp.getWriter();
        if (productMenuList.isEmpty()) {
            ResultVO<ProductMenu> resultVO = new ResultVO<>(0,"error",0,productMenuList);
            String productMenuListJSON = JSONObject.toJSONString(resultVO);
            out.println(productMenuListJSON);
        } else {
            ResultVO<ProductMenu> resultVO = new ResultVO<>(0,"success",count,productMenuList);
            String productMenuListJSON = JSONObject.toJSONString(resultVO);
            out.println(productMenuListJSON);
        }
        out.flush();
        out.close();

    }

    /**
     * 添加产品类型
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        BufferedReader reader = req.getReader();
        Gson gson = new Gson();
        ProductMenu productMenu = gson.fromJson(reader, ProductMenu.class);
        PrintWriter out = resp.getWriter();
        Result result = new Result();
        if (productMenu.getName() == "" || productMenu.getName() == null) {
            result.setStatus(0);
            result.setMessage("error");
            String resultJSON = JSONObject.toJSONString(result);
            out.println(resultJSON);
            out.flush();
            out.close();
        }
        int save = iProductMenuService.save(productMenu);
        if (save < 1) {
            result.setStatus(save);
            result.setMessage("error");
        } else {
            result.setStatus(save);
            result.setMessage("success");
        }
        String resultJSON = JSONObject.toJSONString(result);
        out.println(resultJSON);
        out.flush();
        out.close();

    }

}
